import 'package:safetymonitor/app/components/toast_widget.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/widget_extension.dart';
import 'package:safetymonitor/app/data/common/choice_bean.dart';

/// description: 自定义底部多选弹窗 选择器
/// @Param [dataList] 选择器数据
/// @Param [onConfirm] 确认回调
/// @Param [checkedList] 选中的数据
/// @Param [isMultiple] 是否多选
/// @Param [confirmText] 确认按钮字符
/// @Param [isShowConfirm] 是否展示确定按钮,只有在单选时才能 false
class CustomEquipmentBottomSheet<T extends ChoiceBean> extends StatefulWidget {
  const CustomEquipmentBottomSheet({
    required this.dataList,
    required this.onConfirm,
    required this.isMultiple,
    super.key,
    this.checkedList = const [],
    this.title,
    this.confirmText,
  });

  final List<T> dataList;
  final Function(List<T> value) onConfirm;
  final List<T> checkedList;
  final String? title;
  final bool isMultiple;
  final String? confirmText;


  @override
  State<CustomEquipmentBottomSheet<T>> createState() => _CustomEquipmentBottomSheetState<T>();
}

class _CustomEquipmentBottomSheetState<T extends ChoiceBean> extends State<CustomEquipmentBottomSheet<T>> {
  ///选中的数据
  List<T> checkedList = [];

  @override
  void initState() {
    super.initState();
    //初始化选中的数据
    checkedList.addAll(widget.checkedList);
    if (checkedList.isEmpty) {
      //如果没有选择的数据 就全部置空
      for (var item in widget.dataList) {
        item.setIsChecked(false);
      }
    } else {
      //如果有选择的数据 就把选中的数据置为true
      for (var item in widget.dataList) {
        for (var checkedItem in checkedList) {
          if (item.getId() == checkedItem.getId()) {
            item.setIsChecked(true);
            break;
          } else {
            item.setIsChecked(false);
          }
        }
      }
    }
  }

  /// 点击选择
  void clickToSelect(T item) {
    // 如果是多选
    if (widget.isMultiple) {
      item.setIsChecked(!item.getIsChecked());
      if (item.getIsChecked()) {
        checkedList.add(item);
      } else {
        checkedList.removeWhere((object) => object.getId() == item.getId());
        // checkedList.remove(item);
      }
    } else {
      for (var items in widget.dataList) {
        items.setIsChecked(false);
      }
      checkedList.clear();
      item.setIsChecked(true);
      checkedList.add(item);
      if(!widget.isMultiple) {
        Get.back();
        widget.onConfirm(checkedList);
      }

    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          gradient: const LinearGradient(
              //渐变位置
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              //渐变颜色[始点颜色, 结束颜色]
              colors: [Color(0xffE7EAFF), Color(0xffFFFFFF), Color(0xffFFFFFF)]),
          borderRadius: BorderRadius.only(topLeft: Radius.circular(10.w), topRight: Radius.circular(10.w))),
      // height: 120.w + (70.w * widget.dataList.length),
      child: Column(
        children: [
          Container(
            margin: EdgeInsets.symmetric(vertical: 20.w, horizontal: 30.w),
            alignment: Alignment.centerLeft,
            child: Text(
              widget.title ?? '请选择',
              style: TextStyle(color: const Color(0xff495061), fontSize: 16.sp, fontWeight: FontWeight.w500),
            ),
          ),
          Expanded(
              child: Center(
            child: widget.dataList.isNotEmpty
                ? SingleChildScrollView(
                    child: Column(
                        children: widget.dataList.map((item) {
                      return Column(
                        mainAxisSize: MainAxisSize.min,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          item == widget.dataList.first
                              ? Container(
                                  height: 1.w,
                                  color: const Color(0xFFE7EAFF),
                                )
                              : Container(),
                          Container(
                              padding: EdgeInsets.symmetric(horizontal: 40.w),
                              height: 56.w,
                              width: AppDimens.sw,
                              alignment: Alignment.center,
                              child: Row(
                                children: [
                                  SizedBox(
                                    width: 20.w,
                                  ),
                                  Expanded(
                                      child: Text(
                                    textAlign: TextAlign.center,
                                    item.getShowName(),
                                    style: TextStyle(color: const Color(0xff495061), fontSize: 16.sp, fontWeight: FontWeight.w500),
                                  )),
                                  item.getIsChecked()
                                      ? const Icon(
                                          Icons.check,
                                          color: AppColors.primary_color,
                                        )
                                      : Container(
                                          width: 20.w,
                                        ),
                                ],
                              )),
                          Divider(
                            height: 1.w,
                            thickness: 1.w,
                            indent: 1.w,
                            endIndent: 1.w,
                            color: const Color(0xFFE7EAFF),
                          )
                        ],
                      ).clickOnce(() {
                        clickToSelect(item);
                      });
                    }).toList()),
                  )
                : Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      SvgPicture.asset('assets/svg/zhiban_empty_bg.svg'),
                      Text(
                        '暂无内容',
                        style: TextStyle(
                          color: const Color(0xFFC7CEE5),
                          fontSize: 14.sp,
                        ),
                      ),
                    ],
                  ),
          )),
          Container(
            height: 8.w,
            width: AppDimens.sw,
            color: const Color(0xFFF2F2F2),
          ),
          AppGaps.vGap4,
          Visibility(
            visible: widget.isMultiple,
            child: Container(
                decoration: BoxDecoration(color: const Color(0xff5C6DFF), borderRadius: BorderRadius.all(Radius.circular(40.w))),
                width: 300.w,
                height: 40.w,
                alignment: Alignment.center,
                child: Text(
                  widget.confirmText ?? '确定',
                  style: TextStyle(fontSize: 18.sp, color: Colors.white),
                )).clickOnce(() {
              if (checkedList.isEmpty) {
                AppToast.showError('请选择!');
                return;
              }
              Get.back();
              widget.onConfirm(checkedList);
            }),
          ),
          AppGaps.vGap4
        ],
      ),
    );
  }
}
